<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2019/4/12
  Time: 15:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" +
            request.getServerName() + ":" +
            request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <center>
        <form method="post" action="subway?opr=add">
        <table>
            <tr>
                <td colspan="2">添加地铁线路详细信息</td>
            </tr>
            <tr>
                <td>线路名称:</td>
                <td>
                    <input type="text" name="subwayname">
                    <span id="subname"></span>
                </td>
            </tr>
            <tr>
                <td>始发站:</td>
                <td><input type="text" name="startstation"></td>
            </tr>
            <tr>
                <td>终点站:</td>
                <td><input type="text" name="endstation"></td>
            </tr>
            <tr>
                <td>途经站点:</td>
                <td><input type="text" name="stationnum"></td>
            </tr>
            <tr>
                <td>首车时间:</td>
                <td><input type="text" name="starttime"></td>
            </tr>
            <tr>
                <td>票价（元）:</td>
                <td><input type="text" name="price"></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="添加">
                    <a href="index.jsp">返回首页</a>
                </td>
            </tr>
        </table>
        </form>
    </center>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript">
    $(function () {
        var isFlag = false;
        var subName = $("#subname");
        var subwayname = $("input[name=subwayname]");
        subwayname.blur(function () {
           $.post("subway","opr=check&subwayname="+ subwayname.val(),function (result) {
               if(result=="exists"){
                   isFlag = true;
                   subName.text("地铁名称已存在");
                   subName.css("color","red");
               }else {
                   isFlag = false;
                   subName.text("可以使用");
                   subName.css("color","green");
               }
           },"text");
        });
       $("form").submit(function () {
           var subwayname = $("input[name=subwayname]").val();
           var startstation = $("input[name=startstation]").val();
           var endstation = $("input[name=endstation]").val();
           var stationnum = $("input[name=stationnum]").val();
           var starttime = $("input[name=starttime]").val();
           var price = $("input[name=price]").val();
           if(subwayname==null || subwayname==""){
               alert("线路名称不能为空");
               return false;
           }
           if(isFlag){
               subName.text("地铁名称已存在");
               subName.css("color","red");
               return false;
           }else {
               subName.text("可以使用");
               subName.css("color","green");
           }
           if(startstation==null || startstation==""){
               alert("始发站不能为空");
               return false;
           }
           if(endstation==null || endstation==""){
               alert("终点站不能为空");
               return false;
           }
           if(stationnum==null || stationnum==""){
               alert("途经站点不能为空");
               return false;
           }
           if(starttime==null || starttime==""){
               alert("首车时间不能为空");
               return false;
           }
           if(price==null || price==""){
               alert("票价不能为空");
               return false;
           }
         return false;
       });
    });
</script>
</body>
</html>
